﻿using Zpb;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using UtilPublic;
using UtilPublic.Serialization;
using UtilWpf;
using gpb = global::Google.Protobuf;

namespace client
{
    internal class ToolHelper
    {
        #region dataGrid绑定类
        //输出此类的所有属性名和属性对应的值
        public static List<UserControlColumnNode> GetUserControlColumnNodeList<T>(T t)
        {
            var result = new List<UserControlColumnNode>();
            if (t == null)
            {
                return result;
            }
            var fields = t.GetType().GetProperties();
            foreach (var item in fields)
            {
                //var objs = item.GetCustomAttributes(typeof(DescAttribute), true);
                //if (objs.Length > 0)
                {
                    var FieldType = item.PropertyType;
                    if (FieldType.IsValueType || FieldType.Name.StartsWith("String"))
                    {
                        //result.Add(new UserControlColumnNode(((DescAttribute)objs[0]).DescStr, item.Name));
                        result.Add(new UserControlColumnNode(item.Name, item.Name));
                    }
                }
            }
            return result;
        }
        private static object lockDataGridShow = new object();
        public void DataGridShowObj<T>(DataGrid dgv, T obj, int 表格列宽 = 200)
        {
            lock (lockDataGridShow)
            {
                dgv.Columns.Clear(); dgv.Items.Clear();
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据", Binding = new Binding("DataDesc"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据项", Binding = new Binding("DataName"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据值", Binding = new Binding("DataValue"), Width = 表格列宽, });

                GetFieldsDescList(obj, out var ListDatas);
                foreach (var item in ListDatas)
                {
                    dynamic ditem = new ExpandoObject();
                    ditem.DataDesc = item.Desc;
                    ditem.DataName = item.Label;
                    ditem.DataValue = item.Value;
                    dgv.Items.Add(ditem);
                }
            }
        }
        public void DataGridShowDict(DataGrid dgv, Dictionary<string, string> dict, int 表格列宽 = 200)
        {
            lock (lockDataGridShow)
            {
                dgv.Columns.Clear(); dgv.Items.Clear();
                dgv.Columns.Add(new DataGridTextColumn() { Header = "Key", Binding = new Binding("Key"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "Value", Binding = new Binding("Value"), Width = 表格列宽, });

                foreach (var kvp in dict)
                {
                    dynamic ditem = new ExpandoObject();
                    ditem.Key = kvp.Key;
                    ditem.Value = kvp.Value;
                    dgv.Items.Add(ditem);
                }
            }
        }
        //输出此类的所有属性名和属性对应的值
        public static void GetFieldsDescList<T>(T t, out List<PropertiesNode> result)
        {
            result = new List<PropertiesNode>();
            if (t == null)
            {
                return;
            }
            var newType = t.GetType();
            foreach (var item in newType.GetRuntimeProperties())
            {
                if (item.Name.Contains("Parser") || item.Name.Contains("Descriptor"))
                {
                    continue;
                }
                var type = item.PropertyType.Name;
                var IsGenericType = item.PropertyType.IsGenericType;
                var list = item.PropertyType.GetInterface("IEnumerable", false);
                //Console.WriteLine($"属性名称：{item.Name}，类型：{type}，值：{item.GetValue(t)}");
                if (type == "RepeatedField`1" || type == "MapField`2")
                {
                    result.Add(new PropertiesNode(item.Name, type, item.GetValue(t)));
                }
                else if (IsGenericType && list != null)
                {
                    var listVal = item.GetValue(t) as IEnumerable<object>;
                    if (listVal == null)
                    {
                        result.Add(new PropertiesNode(item.Name, type, "null"));
                    }
                    else
                    {
                        result.Add(new PropertiesNode(item.Name, type, Jsh.Jstr(listVal)));
                    }
                }
                else
                {
                    result.Add(new PropertiesNode(item.Name, type, item.GetValue(t)));
                }
            }
        }
        #endregion

    }
}
